package com.jxwifi.cloud.quickcleanserver.mycenter;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.basicactivity.BasicActivity;
import com.jxwifi.cloud.quickcleanserver.R;
import com.jxwifi.cloud.quickcleanserver.app.AccountMgr;
import com.jxwifi.cloud.quickcleanserver.app.CleanBasicActivity;
import com.jxwifi.cloud.quickcleanserver.app.StateUtils;
import com.jxwifi.cloud.quickcleanserver.app.UrlPool;
import com.jxwifi.cloud.quickcleanserver.okhttp.AuzHttp;
import com.jxwifi.cloud.quickcleanserver.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickcleanserver.okhttp.Params;
import com.jxwifi.cloud.quickcleanserver.utils.PasswordLogin;
import com.jxwifi.cloud.quickcleanserver.utils.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *
 * Created by Administrator on 2019/5/21.
 */

public class WorkingConditionActivity extends CleanBasicActivity {

    @Bind(R.id.img_working_stop)
    ImageView mImgWorkingStop;
    @Bind(R.id.img_working_statue)
    ImageView mImgWorkingStatue;

    private boolean mIschecked; //true工作，false休息

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_working_condition);
        ButterKnife.bind(this);
        openEventBus();
        initView();
    }

    public void initView(){
        if (StateUtils.wait_order.equals(AccountMgr.getacceptStatus(mBaseContext))){//工作中
            mImgWorkingStatue.setBackgroundResource(R.mipmap.checked_ok);
            mImgWorkingStop.setBackgroundResource(R.mipmap.checked_un);
            mIschecked=true;
        }else if (StateUtils.close_order.equals(AccountMgr.getacceptStatus(mBaseContext))){//休息中
            mImgWorkingStatue.setBackgroundResource(R.mipmap.checked_un);
            mImgWorkingStop.setBackgroundResource(R.mipmap.checked_ok);
            mIschecked=false;
        }
    }

    @OnClick({R.id.rel_img_back_pressed,R.id.tv_working_confirm,R.id.rel_working_statue,R.id.rel_working_stop})
    void clickCK(View v){
        switch (v.getId()){
            case R.id.rel_img_back_pressed:
                onBackPressed();
                break;
            //工作状态
            case R.id.rel_working_statue:
                mImgWorkingStatue.setBackgroundResource(R.mipmap.checked_ok);
                mImgWorkingStop.setBackgroundResource(R.mipmap.checked_un);
                mIschecked=true;
                break;
            //休息状态
            case R.id.rel_working_stop:
                mImgWorkingStop.setBackgroundResource(R.mipmap.checked_ok);
                mImgWorkingStatue.setBackgroundResource(R.mipmap.checked_un);
                mIschecked=false;
                break;
            case R.id.tv_working_confirm:
                initdata();
                break;
        }
    }

    public void initdata(){
        Params params=new Params();
        params.add("isWork",mIschecked);
        AuzHttp.get(UrlPool.UPDATE_ACCEPT_STATUS, params, new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                if (code==0){
                    Toast toast =Toast.makeText(mBaseContext, "修改成功", Toast.LENGTH_SHORT);
                    ToastUtils.showMyToast(toast,3000);
                    finish();
                }else{
                    PasswordLogin.passwordlogin(mBaseContext,TAG,AccountMgr.gettell(mBaseContext),AccountMgr.getpassword(mBaseContext));
                    if (PasswordLogin.mIsLogin){
                        initdata();
                    }
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                Toast toast =Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast,3000);
            }
        },TAG);
    }

}
